package com.levy.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.levy.entity.LoginLog;
import com.levy.entity.User;
import com.levy.mapper.LoginLogMapper;
import com.levy.mapper.UserMapper;
import com.levy.service.UserService;

/**
 * @author liwei515
 */
@Service
public class UserServiceImpl implements UserService {

  @Autowired
  private UserMapper userMapper;

  @Autowired
  private LoginLogMapper loginLogMapper;

  @Override
  public boolean hasMatchUser(String userName, String password) {
    int hasUser = userMapper.getMatchCount(userName, password);
    return hasUser > 0;
  }

  @Override
  public User findUserByUserName(String userName) {
    return userMapper.findUserByUserName(userName);
  }

  @Override
  public void loginSuccess(User user) {
    user.setCredits(5 + user.getCredits());
    LoginLog loginLog = new LoginLog();
    loginLog.setUserId(user.getUserId());
    loginLog.setLoginDate(user.getLastVisit());
    loginLog.setIp(user.getLastIp());
    userMapper.updateLoginInfo(user);
    loginLogMapper.insertLoginLog(loginLog);
  }
}
